﻿#if NETSTANDARD2_0 || NETCOREAPP3_0
using Longbow.Configuration;

namespace Microsoft.Extensions.Configuration
{
    /// <summary>
    /// 配置文件配置小节映射操作类
    /// </summary>
    public static class ConfigurationExtensions
    {
        /// <summary>
        /// 通过类型获得配置小节 读取顺序 Key > Name > FullName > ProviderAlias
        /// </summary>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static IConfigurationSection GetSection<T>(this IConfiguration configuration)
        {
            IConfigurationSection section = null;
            var providerType = typeof(T);
            section = configuration.GetSection(providerType.Name);
            if (!section.Exists()) section = configuration.GetSection(providerType.FullName);

            if (!section.Exists())
            {
                var alias = ConfigurationOptionsAliasUtilities.GetAlias(providerType);
                if (!string.IsNullOrEmpty(alias)) section = configuration.GetSection(alias);
            }
            return section;
        }
    }
}
#endif
